package swing.chapter03;

import swing.util.SwingConsle;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Faces extends JFrame {
    private static Icon[] faces;
    private JButton jb, jb2 = new JButton("Disable");
    private boolean mad = false;

    public Faces(){
        faces = new Icon[]{
          new ImageIcon(getClass().getResource("/png/f1.png")),
          new ImageIcon(getClass().getResource("/png/f2.png")),
          new ImageIcon(getClass().getResource("/png/f3.png")),
          new ImageIcon(getClass().getResource("/png/f4.png")),
          new ImageIcon(getClass().getResource("/png/f5.png"))
        };

        jb = new JButton("JButton", faces[3]);
        setLayout(new FlowLayout());
        jb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(mad){
                    jb.setIcon(faces[3]);
                }else{
                    jb.setIcon(faces[0]);
                }
                mad = !mad;
                jb.setVerticalAlignment(JButton.TOP);
                jb.setHorizontalAlignment(JButton.LEFT);
            }
        });
        jb.setRolloverEnabled(true);
        jb.setRolloverIcon(faces[1]);
        jb.setPressedIcon(faces[2]);
        jb.setDisabledIcon(faces[4]);
        // 组件提示，鼠标悬浮一端时间就会显示提示文字
        jb.setToolTipText("Yow!");
        add(jb);

        jb2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(jb.isEnabled()){
                    jb.setEnabled(false);
                    jb2.setText("Enable");
                }else{
                    jb.setEnabled(true);
                    jb2.setText("Disable");
                }
            }
        });
        add(jb2);
    }

    public static void main(String[] args) {
        SwingConsle.run(new Faces(), 800, 500);
    }
}
